package generics;

/**
 * @author 杨宇
 * @version 1.0 2016/10/20
 */
public class E04_Sequence {
    public static void main(String[] args) {
        Sequence<String> sequence = new Sequence<>(10);
        for(int i = 0; i < 10; i++)
            sequence.add(Integer.toString(i));
        Selector selector = sequence.selector();
        while(!selector.end()) {
            System.out.print(selector.current() + " ");
            selector.next();
        }
    }
}

class Sequence<T> {
    private Object[] items;
    private int next = 0;

    public Sequence(int size) {
        items = new Object[size];
    }

    public void add(T t) {
        if (next < items.length) {
            items[next++] = t;
        }
    }
    private class SequenceSelector implements Selector {
        private int i = 0;
        public boolean end() { return i == items.length; }
        public Object current() { return items[i]; }
        public void next() { if(i < items.length) i++; }
    }

    public Selector selector() {
        return new SequenceSelector();
    }
}

interface Selector {
    boolean end();
    Object current();
    void next();
}